package com.cmc.cloud.cmclink.doc.service.impl.multimodaltransport;

import com.cmc.cloud.cmclink.doc.api.document.emums.ExImTypeEnum;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BargeMultiCalculationErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.MultiFinishDO;
import com.cmc.cloud.cmclink.doc.mapper.MultiFinishMapper;
import com.cmc.cloud.cmclink.doc.rpcservice.VoyScheduleService;
import com.cmc.cloud.cmclink.doc.service.multimodaltransport.BargeMultiCalculationService;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multiop.BargeMultiCalculationReqVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multiop.BargeMultiCalculationRespVO;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multiop.MultiFinishOperationReqVO;
import com.cmc.cloud.cmclink.svl.api.base.enums.DateKindEnum;
import com.cmc.cloud.cmclink.svl.api.base.enums.SystemEnums;
import com.cmc.cloud.cmclink.svl.api.base.req.VoyQueryReqDTO;
import com.cmc.cloud.cmclink.svl.api.base.resp.VoyQueryRespDTO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class BargeMultiCalculationServiceImpl implements BargeMultiCalculationService {

    @Resource
    private VoyScheduleService voyScheduleService;
    @Resource
    private MultiFinishMapper multiFinishMapper;

    private static final String FINISH_FLAG = "Y";

    @Override
    public List<BargeMultiCalculationRespVO> queryImExBargeMultiCalculation(BargeMultiCalculationReqVO reqVo) {
        if (ExImTypeEnum.EX.getValue().equals(reqVo.getImExType().toUpperCase())){
            //出口用PTA
            List<VoyQueryRespDTO> list = null;
            if (!ObjectUtils.isEmpty(reqVo.getStartTime()) && !ObjectUtils.isEmpty(reqVo.getEndTime())){
                VoyQueryReqDTO voyQueryReqDTO = new VoyQueryReqDTO();
                voyQueryReqDTO.setDateKind(DateKindEnum.SEARCH_PERFORM.getLabel());
                voyQueryReqDTO.setPortCode(reqVo.getTsPort());
                // 目前eta时间查询
                voyQueryReqDTO.setDateType("PTA");
                voyQueryReqDTO.setStartDate(reqVo.getStartTime());
                voyQueryReqDTO.setEndDate(reqVo.getEndTime());
                voyQueryReqDTO.setSystemNumber(SystemEnums.DOC.getSystemNumber());
                //查询船期数据
                list = voyScheduleService.listByCondition(voyQueryReqDTO);
            }

            List<BargeMultiCalculationRespVO> bargeMultiCalculationRespVos = multiFinishMapper.selectExBargeMultiCalculation(reqVo, list);
            if (!ObjectUtils.isEmpty(bargeMultiCalculationRespVos) && !ObjectUtils.isEmpty(reqVo.getLinerVesselCode()) && !ObjectUtils.isEmpty(reqVo.getLinerVoyage())){
                bargeMultiCalculationRespVos.stream().forEach(t-> {
                    t.setLinerVesselCode(reqVo.getLinerVesselCode());
                    t.setLinerVoyage(reqVo.getLinerVoyage());
                });
            }
            return bargeMultiCalculationRespVos;


        } else if (ExImTypeEnum.IM.getValue().equals(reqVo.getImExType().toUpperCase())) {
            //进口用ETA
            List<VoyQueryRespDTO> list = null;
            if (!ObjectUtils.isEmpty(reqVo.getStartTime()) && !ObjectUtils.isEmpty(reqVo.getEndTime())){
                VoyQueryReqDTO voyQueryReqDTO = new VoyQueryReqDTO();
                voyQueryReqDTO.setDateKind(DateKindEnum.SEARCH_PERFORM.getLabel());
                voyQueryReqDTO.setPortCode(reqVo.getTsPort());
                // 目前eta时间查询
                voyQueryReqDTO.setDateType("ETA");
                voyQueryReqDTO.setStartDate(reqVo.getStartTime());
                voyQueryReqDTO.setEndDate(reqVo.getEndTime());
                voyQueryReqDTO.setSystemNumber(SystemEnums.DOC.getSystemNumber());
                //查询船期数据
                list = voyScheduleService.listByCondition(voyQueryReqDTO);
            }

            List<BargeMultiCalculationRespVO> bargeMultiCalculationRespVos = multiFinishMapper.selectImBargeMultiCalculation(reqVo, list);
            if (!ObjectUtils.isEmpty(bargeMultiCalculationRespVos) && !ObjectUtils.isEmpty(reqVo.getLinerVesselCode()) && !ObjectUtils.isEmpty(reqVo.getLinerVoyage())){
                bargeMultiCalculationRespVos.stream().forEach(t-> {
                    t.setLinerVesselCode(reqVo.getLinerVesselCode());
                    t.setLinerVoyage(reqVo.getLinerVoyage());
                });
            }

            return bargeMultiCalculationRespVos;
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public boolean multiFinishOperation(MultiFinishOperationReqVO multiFinishOperationReqVo) {

        LambdaQueryWrapperX<MultiFinishDO> multiFinishDoLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        multiFinishDoLambdaQueryWrapperX.eq(MultiFinishDO::getFinishFlag,FINISH_FLAG)
                .eq(MultiFinishDO::getTsPort,multiFinishOperationReqVo.getTsPort())
                .eq(MultiFinishDO::getImExType,multiFinishOperationReqVo.getImExType().toUpperCase())
                .eq(MultiFinishDO::getLinerVesselCode,multiFinishOperationReqVo.getLinerVesselCode())
                .eq(MultiFinishDO::getLinerVoyage,multiFinishOperationReqVo.getLinerVoyage());
        boolean exists = multiFinishMapper.exists(multiFinishDoLambdaQueryWrapperX);
        if (exists){
            throw ServiceExceptionUtil.exception(BargeMultiCalculationErrorCodeConstants.MULTI_FINISHED);
        }
        Boolean existValidVslVoy = multiFinishMapper.isExistValidVslVoy(multiFinishOperationReqVo.getImExType().toUpperCase(), multiFinishOperationReqVo);

        if (ObjectUtils.isEmpty(existValidVslVoy) || !existValidVslVoy){
            throw ServiceExceptionUtil.exception(BargeMultiCalculationErrorCodeConstants.NOT_VALID_VSLVOY);
        }

        MultiFinishDO multiFinishDo = new MultiFinishDO();
        multiFinishDo.setFinishFlag(FINISH_FLAG);
        multiFinishDo.setImExType(multiFinishOperationReqVo.getImExType().toUpperCase());
        multiFinishDo.setTsPort(multiFinishOperationReqVo.getTsPort());
        multiFinishDo.setLinerVoyage(multiFinishOperationReqVo.getLinerVoyage());
        multiFinishDo.setLinerVesselCode(multiFinishOperationReqVo.getLinerVesselCode());
        multiFinishDo.setLockDt(LocalDateTime.now());
        multiFinishMapper.insert(multiFinishDo);
        return true;
    }
}
